# -*- coding: utf-8 -*- 
import tkinter as tk


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.firstnum = ''
        self.secondnum = ''
        self.operators = ''
        self.answer = ''
        self.contents = tk.StringVar()


        self.txtDisplay = tk.Entry(self)
        self.contents.set('0')
        self.txtDisplay["textvariable"] = self.contents
        self.txtDisplay["justify"] = "right"
        # self.txtDisplay.pack(side="top",padx=5,pady=5)
        self.txtDisplay.grid(row=0, column=0, columnspan=4, padx=5, pady=5, ipadx=40, ipady=5)

        # btnBackSp
        self.btnBackSp = tk.Button(self)
        self.btnBackSp["text"] = "X"
        self.btnBackSp["width"] = 6
        self.btnBackSp["height"] = 2
        self.btnBackSp["command"] = self.on_btnBackSp_click
        # self.btnBackSp.pack(side="left", padx=5, pady=5)
        self.btnBackSp.grid(row=1, column=0, padx=5, pady=3, sticky='W')

        # btnCE
        self.btnCE = tk.Button(self)
        self.btnCE["text"] = "CE"
        self.btnCE["width"] = 6
        self.btnCE["height"] = 2
        self.btnCE["command"] = self.on_btnCE_click
        # self.btnCE.pack(side="left", padx=3, pady=3)
        self.btnCE.grid(row=1, column=1, sticky='W')

        # btnC
        self.btnC = tk.Button(self)
        self.btnC["text"] = "C"
        self.btnC["width"] = 6
        self.btnC["height"] = 2
        self.btnC["command"] = self.on_btnC_click
        # self.btnC.pack(side="left", padx=3, pady=3)
        self.btnC.grid(row=1, column=2, sticky='W')

        # btnPM
        self.btnPM = tk.Button(self)
        self.btnPM["text"] = "±"
        self.btnPM["width"] = 5
        self.btnPM["height"] = 2
        # self.btnPM["font"] = ('Helvetica', '6')
        self.btnPM["font"] = ('Helvetica', '12')
        self.btnPM["command"] = self.on_btnPM_click
        # self.btnPM.pack(side="left", padx=3, pady=3)
        self.btnPM.grid(row=1, column=3, sticky='W')

        # btn7
        self.btn7 = tk.Button(self)
        self.btn7["text"] = "7"
        self.btn7["width"] = 6
        self.btn7["height"] = 2
        self.btn7["command"] = self.on_btn7_click
        # self.btn7.pack(side="bottom", padx=5, pady=5)
        self.btn7.grid(row=2, column=0, padx=5, pady=3, sticky='W')

        # btn8
        self.btn8 = tk.Button(self)
        self.btn8["text"] = "8"
        self.btn8["width"] = 6
        self.btn8["height"] = 2
        self.btn8["command"] = self.on_btn8_click
        # self.btn8.pack(side="left", padx=5, pady=3)
        self.btn8.grid(row=2, column=1, sticky='W')

        # btn9
        self.btn9 = tk.Button(self)
        self.btn9["text"] = "9"
        self.btn9["width"] = 6
        self.btn9["height"] = 2
        self.btn9["command"] = self.on_btn9_click
        # self.btn9.pack(side="bottom", padx=5, pady=5)
        self.btn9.grid(row=2, column=2, sticky='W')

        # btnPlus
        self.btnPlus = tk.Button(self)
        self.btnPlus["text"] = "+"
        self.btnPlus["width"] = 5
        self.btnPlus["height"] = 2
        self.btnPlus["font"] = ('Helvetica', '12')
        self.btnPlus["command"] = self.on_btnPlus_click
        # self.btn8.pack(side="bottom", padx=5, pady=5)
        self.btnPlus.grid(row=2, column=3, sticky='W')

        # btn4
        self.btn4 = tk.Button(self)
        self.btn4["text"] = "4"
        self.btn4["width"] = 6
        self.btn4["height"] = 2
        self.btn4["command"] = self.on_btn4_click
        # self.btn4.pack(side="left", padx=5, pady=3)
        self.btn4.grid(row=3, column=0, padx=5, pady=3, sticky='W')

        # btn5
        self.btn5 = tk.Button(self)
        self.btn5["text"] = "5"
        self.btn5["width"] = 6
        self.btn5["height"] = 2
        self.btn5["command"] = self.on_btn5_click
        # self.btn5.pack(side="left", padx=5, pady=3)
        self.btn5.grid(row=3, column=1, sticky='W')

        # btn6
        self.btn6 = tk.Button(self)
        self.btn6["text"] = "6"
        self.btn6["width"] = 6
        self.btn6["height"] = 2
        self.btn6["command"] = self.on_btn6_click
        # self.btn6.pack(side="left", padx=5, pady=3)
        self.btn6.grid(row=3, column=2, sticky='W')

        # btnMinus
        self.btnMinus = tk.Button(self)
        self.btnMinus["text"] = "-"
        self.btnMinus["width"] = 5
        self.btnMinus["height"] = 2
        self.btnMinus["font"] = ('Helvetica', '12')
        self.btnMinus["command"] = self.on_btnMinus_click
        # self.btnMinus.pack(side="left", padx=5, pady=3)
        self.btnMinus.grid(row=3, column=3, sticky='W')

        # btn1
        self.btn1 = tk.Button(self)
        self.btn1["text"] = "1"
        self.btn1["width"] = 6
        self.btn1["height"] = 2
        self.btn1["command"] = self.on_btn1_click
        # self.btn1.pack(side="left", padx=5, pady=3)
        self.btn1.grid(row=4, column=0, padx=5, pady=3, sticky='W')

        # btn2
        self.btn2 = tk.Button(self)
        self.btn2["text"] = "2"
        self.btn2["width"] = 6
        self.btn2["height"] = 2
        self.btn2["command"] = self.on_btn2_click
        # self.btn2.pack(side="left", padx=5, pady=3)
        self.btn2.grid(row=4, column=1, sticky='W')

        # btn3
        self.btn3 = tk.Button(self)
        self.btn3["text"] = "3"
        self.btn3["width"] = 6
        self.btn3["height"] = 2
        self.btn3["command"] = self.on_btn3_click
        # self.btn3.pack(side="left", padx=5, pady=3)
        self.btn3.grid(row=4, column=2, sticky='W')

        # btnMulti
        self.btnMulti = tk.Button(self)
        self.btnMulti["text"] = "*"
        self.btnMulti["width"] = 5
        self.btnMulti["height"] = 2
        self.btnMulti["font"] = ('Helvetica', '12')
        self.btnMulti["command"] = self.on_btnMulti_click
        # self.btnMulti.pack(side="left", padx=5, pady=3)
        self.btnMulti.grid(row=4, column=3, sticky='W')

        # self.quit = tk.Button(self, text="QUIT", fg="red",
        #                       command=self.master.destroy)
        # self.quit.pack(side="bottom")

        # btn0
        self.btn0 = tk.Button(self)
        self.btn0["text"] = "0"
        self.btn0["width"] = 6
        self.btn0["height"] = 2
        self.btn0["command"] = self.on_btn0_click
        # self.btn0.pack(side="left", padx=5, pady=3)
        self.btn0.grid(row=5, column=0, padx=5, pady=3, sticky='W')

        # btnDot
        self.btnDot = tk.Button(self)
        self.btnDot["text"] = "."
        self.btnDot["width"] = 6
        self.btnDot["height"] = 2
        self.btnDot["command"] = self.on_btnDot_click
        # self.btnDot.pack(side="left", padx=5, pady=3)
        self.btnDot.grid(row=5, column=1, sticky='W')

        # btnEqual
        self.btnEqual = tk.Button(self)
        self.btnEqual["text"] = "="
        self.btnEqual["width"] = 6
        self.btnEqual["height"] = 2
        self.btnEqual["command"] = self.on_btnEqual_click
        # self.btnEqual.pack(side="left", padx=5, pady=3)
        self.btnEqual.grid(row=5, column=2, sticky='W')

        # btnDiv
        self.btnDiv = tk.Button(self)
        self.btnDiv["text"] = "/"
        self.btnDiv["width"] = 5
        self.btnDiv["height"] = 2
        self.btnDiv["font"] = ('Helvetica', '12')
        self.btnDiv["command"] = self.on_btnDiv_click
        # self.btnDiv.pack(side="left", padx=5, pady=3)
        self.btnDiv.grid(row=5, column=3, sticky='W')

    def on_btnBackSp_click(self):
        if len(self.contents.get()) == 1:
            self.contents.set('')
            self.txtDisplay["textvariable"] = self.contents
        else:
            self.contents.set(self.contents.get()[:-1])
            self.txtDisplay["textvariable"] = self.contents

    def on_btnCE_click(self):
        self.contents.set('0')
        self.txtDisplay["textvariable"] = self.contents
        self.firstnum = ''
        self.secondnumnum = ''
        self.operators = ''

    def on_btnC_click(self):
        self.contents.set('')
        self.txtDisplay["textvariable"] = self.contents
        self.firstnum = ''
        self.secondnumnum = ''
        self.operators = ''

    def on_btnPM_click(self):
        if self.txtDisplay.get() != '0':
            self.contents.set(str(-1 * float(self.contents.get())))
            self.txtDisplay["textvariable"] = self.contents


    def on_btn7_click(self):
        print(self.btn7['text'])
        print(type(self.btn7["text"]))
        print(type(self.contents.get()))
        # print(self.txtDisplay.get())
        if self.txtDisplay.get() == '0':
            self.contents.set(self.btn7["text"])
            self.txtDisplay["textvariable"] = self.contents
            # self.txtDisplay.setvar(self.contents)
        else:
            self.contents.set(self.contents.get() + self.btn7["text"])
            self.txtDisplay["textvariable"] = self.contents

    def on_btn8_click(self):
        if self.txtDisplay.get() == '0':
            self.contents.set(self.btn8["text"])
            self.txtDisplay["textvariable"] = self.contents
            # self.txtDisplay.setvar(self.contents)
        else:
            self.contents.set(self.contents.get() + self.btn8["text"])
            self.txtDisplay["textvariable"] = self.contents

    def on_btn9_click(self):
        if self.txtDisplay.get() == '0':
            self.contents.set(self.btn9["text"])
            self.txtDisplay["textvariable"] = self.contents
            # self.txtDisplay.setvar(self.contents)
        else:
            self.contents.set(self.contents.get() + self.btn9["text"])
            self.txtDisplay["textvariable"] = self.contents

    def on_btnPlus_click(self):
        self.firstnum = self.contents.get()
        self.operators = '+'
        self.contents.set('')
        self.txtDisplay["textvariable"] = self.contents

    def on_btn4_click(self):
        if self.txtDisplay.get() == '0':
            self.contents.set(self.btn4["text"])
            self.txtDisplay["textvariable"] = self.contents
            # self.txtDisplay.setvar(self.contents)
        else:
            self.contents.set(self.contents.get() + self.btn4["text"])
            self.txtDisplay["textvariable"] = self.contents

    def on_btn5_click(self):
        if self.txtDisplay.get() == '0':
            self.contents.set(self.btn5["text"])
            self.txtDisplay["textvariable"] = self.contents
            # self.txtDisplay.setvar(self.contents)
        else:
            self.contents.set(self.contents.get() + self.btn5["text"])
            self.txtDisplay["textvariable"] = self.contents

    def on_btn6_click(self):
        if self.txtDisplay.get() == '0':
            self.contents.set(self.btn6["text"])
            self.txtDisplay["textvariable"] = self.contents
            # self.txtDisplay.setvar(self.contents)
        else:
            self.contents.set(self.contents.get() + self.btn6["text"])
            self.txtDisplay["textvariable"] = self.contents

    def on_btnMinus_click(self):
        self.firstnum = self.contents.get()
        self.operators = '-'
        self.contents.set('')
        self.txtDisplay["textvariable"] = self.contents

    def on_btn1_click(self):
        if self.txtDisplay.get() == '0':
            self.contents.set(self.btn1["text"])
            self.txtDisplay["textvariable"] = self.contents
            # self.txtDisplay.setvar(self.contents)
        else:
            self.contents.set(self.contents.get() + self.btn1["text"])
            self.txtDisplay["textvariable"] = self.contents

    def on_btn2_click(self):
        if self.txtDisplay.get() == '0':
            self.contents.set(self.btn2["text"])
            self.txtDisplay["textvariable"] = self.contents
            # self.txtDisplay.setvar(self.contents)
        else:
            self.contents.set(self.contents.get() + self.btn2["text"])
            self.txtDisplay["textvariable"] = self.contents

    def on_btn3_click(self):
        if self.txtDisplay.get() == '0':
            self.contents.set(self.btn3["text"])
            self.txtDisplay["textvariable"] = self.contents
            # self.txtDisplay.setvar(self.contents)
        else:
            self.contents.set(self.contents.get() + self.btn3["text"])
            self.txtDisplay["textvariable"] = self.contents

    def on_btnMulti_click(self):
        self.firstnum = self.contents.get()
        self.operators = '*'
        self.contents.set('')
        self.txtDisplay["textvariable"] = self.contents

    def on_btn0_click(self):
        if self.txtDisplay.get() == '0':
            self.contents.set(self.btn0["text"])
            self.txtDisplay["textvariable"] = self.contents
            # self.txtDisplay.setvar(self.contents)
        else:
            self.contents.set(self.contents.get() + self.btn0["text"])
            self.txtDisplay["textvariable"] = self.contents

    def on_btnDot_click(self):
        # if (POS('.', txtDisplay.Text) <> 0) then
        #     exit
        # else
        #     txtDisplay.Text := txtDisplay.Text + btnPoint.Caption;
        tmpStr = self.contents.get()  
        if tmpStr.count('.') != 0 and tmpStr.index('.') != 0:
            pass
        else:
            self.contents.set(self.contents.get() + '.')
            self.txtDisplay["textvariable"] = self.contents

    def on_btnEqual_click(self):
        self.secondnum = self.contents.get()
        if self.operators == '+':
            self.answer = str(float(self.firstnum) + float(self.secondnum))
            self.txtDisplay["textvariable"] = self.contents.set(self.answer)
        elif self.operators == '-':
            self.answer = str(float(self.firstnum) - float(self.secondnum))
            self.txtDisplay["textvariable"] = self.contents.set(self.answer)
        elif self.operators == '*':
            self.answer = str(float(self.firstnum) * float(self.secondnum))
            self.txtDisplay["textvariable"] = self.contents.set(self.answer)
        elif self.operators == '/' and float(self.secondnum) != 0.0:
            self.answer = str(float(self.firstnum) / float(self.secondnum))
            self.txtDisplay["textvariable"] = self.contents.set(self.answer)            

    def on_btnDiv_click(self):
        self.firstnum = self.contents.get()
        self.operators = '/'
        self.contents.set('')
        self.txtDisplay["textvariable"] = self.contents


root = tk.Tk()
root.title('calculaotor')
root.geometry("250x310+200+300")
root.resizable(False, False)
app = Application(master=root)
app.mainloop()
